from flask import Flask
from flask import render_template
from flask import request
from flask import redirect, url_for
from werkzeug.utils import secure_filename

app = Flask(__name__)


@app.route("/")
def index():
    return "<h1>欢迎来到首页</h1>"


@app.route("/upload/", methods=["GET", "POST"])
def upload():
    error = None
    if request.method == "POST":
        head = request.files["head"]
        if head.filename:
            head.save(f'media/head/{secure_filename(head.filename)}')
            return redirect(url_for("index"))
        else:
            error = "请选择文件"
    return render_template("upload.html", error=error)


def main():
    app.run(debug=True)


if __name__ == '__main__':
    main()
